﻿using System;
using System.Collections.Generic;
using System.Text;
using CharacterTrailLibrary.Exceptions;
using CharacterTrailLibrary.Persist.DAL.Orm;
using Telegnosis.Framework.Base;

namespace CharacterTrailLibrary.Persist.DAL.Dao
{
    public static class SelectStatementBuilderFactory
    {
        public static ISelectStatementBuilder GetSelectStatementBuilder(string className)
        {
            string typeName = "CharacterTrailLibrary.Persist.DAL.Dao." + className + "SelectStatementBuilder";
            Type type = Type.GetType(typeName);
            if(type != null)
            {
                var obj = type.Assembly.CreateInstance(typeName, true);
                return obj as ISelectStatementBuilder;
            }
            else
            {
                throw new AppException(String.Format("不存在类型{0}！",className));
            }
        }
    }

    public interface ISelectStatementBuilder
    {
        string GetSelectStatement();
    }

    public class BomMainSelectStatementBuilder : ISelectStatementBuilder
    {
        private const string Sql = "select * from ( "+
            " SELECT ProductItemId,i.FNumber as ProductItemNumber,i.FName as ProductItemName,ProductVersion,CheckStatus,CheckId,CheckDate,UnCheckId,UnCheckDate,ImportStatus,ImportId,ImportDate,ExtChar1,ExtChar2,ExtChar3,ExtChar4,ExtChar5,ExtChar6,ExtChar7,ExtChar8,ExtChar9,ExtChar10,ExtNumeric1,ExtNumeric2,ExtNumeric3,ExtNumeric4,ExtNumeric5,ExtNumeric6,ExtNumeric7,ExtNumeric8,ExtNumeric9,ExtNumeric10 FROM TBomMain m,VIcItemQuery i where m.ProductItemId = i.FItemId "+
            " ) tmp ";
        public string GetSelectStatement()
        {
            return Sql;
        }
    }

    public class BomEntrySelectStatementBuilder : ISelectStatementBuilder
    {
        private const string Sql = "select * from ( "+
                " select tmp.*,k.FNumber as MaterialItemNumber,k.FName as MaterialItemName from ( "+
                " SELECT ProductItemId,i.FNumber as ProductItemNumber,i.FName as ProductItemName,ProductVersion,MaterialItemId,Idx,Qty,ProcedureNo,ProcedureName,ImportStatus,ImportId,ImportDate,ExtChar1,ExtChar2,ExtChar3,ExtChar4,ExtChar5,ExtChar6,ExtChar7,ExtChar8,ExtChar9,ExtChar10,ExtNumeric1,ExtNumeric2,ExtNumeric3,ExtNumeric4,ExtNumeric5,ExtNumeric6,ExtNumeric7,ExtNumeric8,ExtNumeric9,ExtNumeric10 FROM TBomEntry m,VIcItemQuery i  WHERE m.ProductItemId=i.FItemId "+
                " ) tmp ,VIcItemQuery k where tmp.MaterialItemId = k.FItemId  "+
                " ) tmp2  ";
        public string GetSelectStatement()
        {
            return Sql;
        }
    }

    public class ProductDetailSelectStatementBuilder : ISelectStatementBuilder
    {
        private const string Sql = "select * from ( "+
                    " SELECT RowGuid,ParentProductItemBarCode,ProcedureNo,ProcedureName,ChildItemBarCode,ChildItemId,i.FNumber as ChildItemNumber,i.FName as ChildItemName,Idx,UseStatus,InputId,InputDate,CheckId,CheckDate,ExtChar1,ExtChar2,ExtChar3,ExtChar4,ExtChar5,ExtChar6,ExtChar7,ExtChar8,ExtChar9,ExtChar10,ExtNumeric1,ExtNumeric2,ExtNumeric3,ExtNumeric4,ExtNumeric5,ExtNumeric6,ExtNumeric7,ExtNumeric8,ExtNumeric9,ExtNumeric10 FROM TProductDetail m,VIcItemQuery i where m.ChildItemId = i.FItemId "+
                    " ) tmp ";
        public string GetSelectStatement()
        {
            return Sql;
        }
    }

    public class ProductHeaderSelectStatementBuilder : ISelectStatementBuilder
    {
        private const string Sql = "select * from ( "+
                    " SELECT ProductItemBarCode,ProcedureNo,ProcedureName,ProductBaseRowGuid,ProductItemId,i.FNumber as ProductItemNumber, i.FName as ProductItemName,OrderId,UseStatus,TranType,InputId,InputDate,CheckId,CheckDate,ExtChar1,ExtChar2,ExtChar3,ExtChar4,ExtChar5,ExtChar6,ExtChar7,ExtChar8,ExtChar9,ExtChar10,ExtNumeric1,ExtNumeric2,ExtNumeric3,ExtNumeric4,ExtNumeric5,ExtNumeric6,ExtNumeric7,ExtNumeric8,ExtNumeric9,ExtNumeric10 FROM TProductHeader m,VIcItemQuery i where m.ProductItemId = i.FItemId "+
                    " ) tmp ";
        public string GetSelectStatement()
        {
            return Sql;
        }
    }

    public class RepairHeaderSelectStatementBuilder : ISelectStatementBuilder
    {
        private const string Sql = "select * from ( "+
                    " select tmp.*,k.FNumber as NewProductItemNumber,k.FName as NewProductItemName from ( "+
                    " SELECT ProductItemBarCode,ProductBaseRowGuid,ProductItemId,i.FNumber as ProductItemNumber, i.FName as ProductItemName,OrderId,NewProductItemBarCode,NewProductItemId,ProcedureNo,ProcedureName,UseStatus,InputId,InputDate,CheckId,CheckDate,ExtChar1,ExtChar2,ExtChar3,ExtChar4,ExtNumeric1,ExtNumeric2,ExtNumeric3,ExtNumeric4 FROM TRepairHeader  m,VIcItemQuery i where m.ProductItemId = i.FItemId " +
                    " ) tmp ,VIcItemQuery k where tmp.NewProductItemId = k.FItemId "+
                    " ) tmp2  ";
        public string GetSelectStatement()
        {
            return Sql;
        }
    }
}
